package org.springrain.testjpa.config;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.transaction.Transactional;

/**
 * @author springrain
 * @version 1.0
 **/
@Order(0)
@Aspect
@Component
@Lazy(false)
public class RoutingAopAspect {

    @Around("@annotation(transactional)")
    public Object routingWithDataSource(ProceedingJoinPoint joinPoint, Transactional transactional) throws Throwable {
        try {
            DynamicRoutingDataSourceContext.setRoutingDataSource(DynamicRoutingDataSourceContext.MASTER);
            return joinPoint.proceed();
        } finally {
            DynamicRoutingDataSourceContext.removeRoutingDataSource();
        }
    }

}
